package com.beyondsoft.common.activiti.validation;

import org.activiti.bpmn.model.BpmnModel;
import org.activiti.bpmn.model.Process;
import org.activiti.bpmn.model.UserTask;
import org.activiti.validation.ValidationError;
import org.activiti.validation.validator.ProcessLevelValidator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 校验
 *
 * @author zhangyan
 * @version $ Id: UserTaskValidator.java, v 0.1 2020/6/28 14:14 zhangyan Exp $
 */
public class UserTaskValidator extends ProcessLevelValidator {

    @Override
    protected void executeValidation(BpmnModel bpmnModel, Process process, List<ValidationError> errors) {
        //获取模型中所有的用户任务节点
        List<UserTask> userTaskList = process.findFlowElementsOfType(UserTask.class);
        for (UserTask userTask : userTaskList) {
            //获取用户任务指定人
            String assignee = userTask.getAssignee();
            if (StringUtils.isNotBlank(assignee)
                    && CollectionUtils.isEmpty(userTask.getCandidateGroups())
                    && CollectionUtils.isEmpty(userTask.getCandidateUsers())) {
                //向List<ValidationError>添加校验错误结果
                ValidationError validationError = new ValidationError();
                validationError.setProblem(userTask.getName() + "--->没有指定办理人");
                validationError.setWarning(false);
                errors.add(validationError);
            }
        }
    }
}
